'use strict';

const { addFollowerFailInfo, deleteFollowerFailInfo } = require('../utils/ErrorModel');
const { SuccessModel, ErrorModel } = require('../utils/resultModel');

module.exports = app => {
  class ProfileController extends app.Controller {
    async index() {
      // 在session获取用户名
      const { userName } = this.ctx.session.userInfo;
      // 重定向
      await this.ctx.redirect(`/profile/${userName}`);
    };

    // 个人主页
    async profileUser() {
      // 获取当前的用户名
      const { userName: curUserName } = this.ctx.params;
      // console.log(curUserName, this.ctx.session)
      // 获取登录热的用户名
      const { userName } = this.ctx.session.userInfo;
      let isMe = true;
      let curUserInfo;
      // console.log(curUserName)
      if (curUserName !== userName) {
        // 如果不相等说明查看的不是本人信息
        isMe = false;
        // 获取当前用户信息前 先判断这个用户是否存在
        const result = await this.service.user.getUserInfo(curUserName);
        console.log(result)
        if (result) {
          curUserInfo = result.dataValues;
        } else { 
          await this.ctx.render('404.html');
          return;
        }
      } else {
        curUserInfo = this.ctx.session.userInfo;
      }
      // 用户基本信息 关注 粉丝 本人基本信息
      // const userData = {};
      // userData.userInfo = this.ctx.session.userInfo;

      // 获取博客信息 service
      const result = await this.service.blog.getProfileBlogList({ userName: curUserName, pageSize: 5 });

      // 获取粉丝列表
      const { userList: fansList, count: fansCount } = await this.service.userRelation.getUsersByFollowerId(curUserInfo.id);

      // 获取关注列表
      const { userList: followList, count: followerCount } = await this.service.userRelation.getFollower(curUserInfo.id);

      const isMeFollow = fansList.some(function (item) {
        return item.userName === userName;
      });

      console.log(isMeFollow)

      // 最终返回的数据包含 博客信息 用户基本信息
      await this.ctx.render('profile.html', {
        userData: {
          userInfo: curUserInfo,
          fansData: {   // 粉丝
            count: fansCount,
            fansList,
          },
          followData: {   // 关注
            count: followerCount,
            followList,
          },
          isMe,
          isMeFollow,
        },
        blogList: result.blogList,
        pageIndex: 1,   // 当前页码
        pageSize: 5,    // 每页的数量
        count: result.count,  // 微博的总数量
      });
    };

    //关注
    async follow() {
      // 获取id 在session中获取本人id
      const { id: myUserId } = this.ctx.session.userInfo;
      // 获取followerId 前端传递过来的被关注人id post方式提交的数据
      const { followerId } = this.ctx.request.body;
      console.log(myUserId, followerId)
      try {
        // 调用service
        const result = await this.service.userRelation.addFollower(myUserId, followerId);

        // 添加成功
        this.ctx.body = new SuccessModel(result);
      } catch (err) {
        // 添加关注失败
        this.ctx.body = new ErrorModel(addFollowerFailInfo);
      }
    }

    // 取消关注
    async unFollow() {
      const { id: myUserId } = this.ctx.session.userInfo;
      const { followerId } = this.ctx.request.body;
      const result = await this.service.userRelation.deleteFollower(myUserId, followerId);

      if (result) {
        this.ctx.body = new SuccessModel();
        return;
      }
      this.ctx.body = new ErrorModel(deleteFollowerFailInfo);
    }
  }
  return ProfileController;
};